package hflyeer.o2o.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import hflyeer.o2o.biz.service.ShopCategoryService;
import hflyeer.o2o.model.entity.ShopCategory;

/**
 * 店铺类别控制类
 */
@Controller
@RequestMapping("/shopadmin")
public class ShopCategoryController {

	// json和java bean互转
	private ObjectMapper mapper = new ObjectMapper();

	@Autowired
	private ShopCategoryService shopCategoryService;

	/**
	 * 获取店铺分类列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/listshopcategory", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> getShopCategoryList() {
		Map<String, Object> modelMap = new HashMap<>();

		List<ShopCategory> shopCategoryList = shopCategoryService.getShopCategorys();
		modelMap.put("shopCategoryList", shopCategoryList);
		modelMap.put("count", shopCategoryList.size());
		modelMap.put("msg", "店铺分类列表");
		return modelMap;
	}

	/**
	 * 删除店铺分类
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/removeshopcategory")
	@ResponseBody
	private Map<String, Object> removeShopCategoryById(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<>();
		String shopCagtegoryId = request.getParameter("shopCategoryId");
		// 前台传入参数不对
		if (shopCagtegoryId == null) {
			modelMap.put("success", false);
			modelMap.put("msg", "删除失败，请传入店铺分类id");
			return modelMap;
		}

		int res = shopCategoryService.deleteShopCategory(Integer.parseInt(shopCagtegoryId));
		// 判断删除是否成功
		if (res <= 0) {
			// 失败
			modelMap.put("success", false);
			modelMap.put("msg", "删除失败");
		} else {
			// 成功
			modelMap.put("success", true);
			modelMap.put("msg", "删除成功");
		}

		return modelMap;
	}

	/**
	 * 批量添加店铺类别
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addhopcategory")
	@ResponseBody
	private Map<String, Object> batchInsertShopCategoryById(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<>();
		String shopCategoryInfos = request.getParameter("shopCategoryInfo");
		if (shopCategoryInfos == null) {
			modelMap.put("success", false);
			modelMap.put("msg", "请输入添加的店铺分类信息");
			return modelMap;
		}

		try {
			@SuppressWarnings("unchecked")
			List<ShopCategory> shopCategoryList = mapper.readValue(shopCategoryInfos, List.class);
			int res = shopCategoryService.batchAddShopCategory(shopCategoryList);
			// 判断删除是否成功
			if (res <= 0) {
				// 失败
				modelMap.put("success", false);
				modelMap.put("msg", "添加失败");
			} else {
				// 成功
				modelMap.put("success", true);
				modelMap.put("msg", "添加成功");
			}
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			modelMap.put("success", false);
			modelMap.put("msg", e.getMessage());
		}

		return modelMap;
	}
}
